package io.renren.modules.member.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.exception.RRException;
import io.renren.common.utils.BeanJudgeUtil;
import io.renren.modules.member.entity.TStoreMemberTypeEntity;
import io.renren.modules.member.service.TStoreMemberTypeService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.member.entity.TStoreMemberRuleEntity;
import io.renren.modules.member.service.TStoreMemberRuleService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-27 20:40:35
 */
@RestController
@RequestMapping("member/tstorememberrule")
public class TStoreMemberRuleController extends AbstractController {
    @Autowired
    private TStoreMemberRuleService tStoreMemberRuleService;
    @Autowired
    private TStoreMemberTypeService tStoreMemberTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("member:tstorememberrule:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        PageUtils page = tStoreMemberRuleService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 会员规则列表列表
     */
    @GetMapping("/listMemberRule")
    @RequiresPermissions("member:tstorememberrule:listMemberRule")
    public R listMemberRule(@RequestParam Map<String,Object> params){
        params.put("sellerId",getSeller());
        Page<TStoreMemberRuleEntity> page = tStoreMemberRuleService.listMemberRule(params);
        return R.ok().put("data",page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("member:tstorememberrule:info")
    public R info(@PathVariable("id") Long id){
		TStoreMemberRuleEntity tStoreMemberRule = tStoreMemberRuleService.getById(id);
        List<Long> ids = new ArrayList<>();
        ids.add(tStoreMemberRule.getMemberTypeId());
        tStoreMemberRule.setMemberTypeList(ids);
        return R.ok().put("tStoreMemberRule", tStoreMemberRule);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("member:tstorememberrule:save")
    public R save(@RequestBody TStoreMemberRuleEntity tStoreMemberRule){
        List<Long> memberTypeList = tStoreMemberRule.getMemberTypeList();
        List<TStoreMemberRuleEntity> list = new ArrayList<>();
        for (Long type : memberTypeList){
            TStoreMemberTypeEntity one = tStoreMemberTypeService.getOne(new QueryWrapper<TStoreMemberTypeEntity>()
                    .eq("seller_id", getSeller())
                    .eq("id", type)
            );
            if (!BeanJudgeUtil.beanIsNull(one)){
                throw new RRException("会员类型"+"["+one.getMemberTypeName()+"]已经配置过");
            }
            TStoreMemberRuleEntity entity = new TStoreMemberRuleEntity();
            entity.setMemberTypeId(type);
            entity.setSellerId(getSeller());
            entity.setStartIntegral(tStoreMemberRule.getStartIntegral());
            entity.setEndIntegral(tStoreMemberRule.getEndIntegral());
            entity.setExchangeAmount(tStoreMemberRule.getExchangeAmount());
            entity.setExchangeIntegral(tStoreMemberRule.getExchangeIntegral());
            entity.setIntegralClearDate(tStoreMemberRule.getIntegralClearDate());
            entity.setIntegralClearRule(tStoreMemberRule.getIntegralClearRule());
            entity.setIntegralClearRuleStatus(tStoreMemberRule.getIntegralClearRuleStatus());
            entity.setUpgradeRule(tStoreMemberRule.getUpgradeRule());
            entity.setMinQuota(tStoreMemberRule.getMinQuota());
            entity.setDiscount(tStoreMemberRule.getDiscount());
            list.add(entity);
        }
        tStoreMemberRuleService.saveBatch(list);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("member:tstorememberrule:update")
    public R update(@RequestBody TStoreMemberRuleEntity tStoreMemberRule){
        tStoreMemberRule.setMemberTypeId(tStoreMemberRule.getMemberTypeList().get(0));
		tStoreMemberRuleService.updateById(tStoreMemberRule);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("member:tstorememberrule:delete")
    public R delete(@RequestBody Long[] ids){
		tStoreMemberRuleService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
